package com.csw.shuanfa.CodeImprove.designPattern;

/**
 * 定义策略接口
 */
interface Strategy {
    void execute();
}

/**
 * 策略模式
 */
public class StrategyD {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Strategy strategyA = new StrategyA();
        Context context = new Context(strategyA);
        context.doStrategy();

        Strategy strategyB = new StrategyB();
        context.setStrategy(strategyB);
        context.doStrategy();
    }
}

/**
 * 定义具体策略类
 */
class StrategyA implements Strategy {

    @Override
    public void execute() {
        System.out.println("执行策略A");
    }
}

class StrategyB implements Strategy {

    @Override
    public void execute() {
        System.out.println("执行策略B");
    }
}

/**
 * 定义上下文类
 */
class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public void doStrategy() {
        strategy.execute();
    }
}

